package com.miracle.erp.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {

	public static Connection getConnection() throws SQLException,
			ClassNotFoundException {
		Properties properties = PropertiesUtil.getProperties();

		String driver = properties.getProperty("driver");
		String url = properties.getProperty("url");
		String username = properties.getProperty("username");
		String password = properties.getProperty("password");

		Class.forName(driver);
		Connection connection = DriverManager.getConnection(url, username,
				password);
		return connection;
	}

	// ResultSet,PreparedStatement,Statement,Connection 都是 AutoCloseable
	// 的子类,所以因为多态,可以接收他们的对象
	// 所以一个方法就可以
	public static void close(AutoCloseable obj) {
		if (obj != null) {
			try {
				obj.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
